local huajin=fk.CreateSkill{
    name = "steam__huajin",
    tags={Skill.Compulsory}
}

-- 化烬技能
huajin:addEffect(fk.EventPhaseStart,{
    anim_type = "negative",
    can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(self) and player.phase == Player.Finish
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local last_lost = player:getMark("steam__huajin-last_lost")
      local current_hand = #player:getCardIds("h")
      local diff = last_lost - current_hand
      
      if diff ~= 0 then
        if diff > 0 then
          -- 需要摸牌
          player:drawCards(diff, self.name)
        else
          -- 需要弃牌
          local to_discard = room:askForDiscard(player, -diff, -diff, false, self.name, false)
          if #to_discard > 0 then
            room:throwCard(to_discard, self.name, player, player)
          end
  
        end
  
      end
      if math.abs(diff) ~= 1 and diff<0 then
        local targets = table.map(room.alive_players, Util.IdMapper)
        local tos = room:askForChoosePlayers(player, targets, 1, 1, "#steam__huajin-damage", self.name, false)
        room:damage {
          from = player, to = room:getPlayerById(tos[1]),
          damage = 1, skillName = self.name, damageType = fk.FireDamage,
        }
      elseif math.abs(diff) ~= 1 and diff>0 then
        room:damage {
          from = player, to = player,
          damage = 1, skillName = self.name, damageType = fk.FireDamage,
        }
      end
    end,
  })
  
  -- 记录失去牌数的技能
 huajin:addEffect(fk.AfterCardsMove,{
    anim_type = "drawcard",
    mute=true,
    can_refresh = function(self, event, target, player, data)
      if not player:hasSkill(self.name) then return false end
      for _, move in ipairs(data) do
        if move.from == player then
          return true
        end
      end
    end,
    on_refresh = function(self, event, target, player, data)
      local room = player.room
      local lost = 0
      for _, move in ipairs(data) do
        if move.from == player then
         lost=lost+#move.moveInfo
        end
      end
      room:setPlayerMark(player, "steam__huajin-last_lost", lost)
  
    end,
  })

  return huajin
  